package com.android.djlmapp.activity.kuandai;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.djlmapp.R;
import com.android.djlmapp.adapter.kuandai.KuanDaiFuJiaCanShuAdapter;
import com.android.djlmapp.application.MyApplication;
import com.android.djlmapp.base.BaseActivity;
import com.android.djlmapp.http.GlobalUrls;
import com.android.djlmapp.model.kuandai.KuanDaiFuJiaInfo;
import com.android.djlmapp.tools.global.ToastUtils;
import com.google.gson.Gson;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import org.refresh_view.pullrecyclerview.PullRecyclerView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 宽带SIM卡号
 */
public class KuanDaiFuJiaCanShuActivity extends Activity {

    @BindView(R.id.sim_search)
    EditText simSearch;
    @BindView(R.id.kuandai_sim_recycler)
    PullRecyclerView kuandaiSimRecycler;
    @BindView(R.id.kd_fjcs_name)
    TextView kdFjcsName;
    @BindView(R.id.rl_progress)
    RelativeLayout rlProgress;

    private KuanDaiFuJiaCanShuAdapter mKuanDaiFuJiaCanShuAdapter;
    private int canShuType;
    private String mUrl;
    private String keyValue;
    private boolean isFzrName = false;
    private String gsqdName = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_kuan_dai_sim_num);
        ButterKnife.bind(this);
        initView();
    }

    protected void initView() {
        canShuType = getIntent().getIntExtra("CanShuType", 1);
        String fjcsName = "";
        switch (canShuType) {
            case 1:
                fjcsName = "归属渠道名称";
                isFzrName = false;
                mUrl = GlobalUrls.GET_GSQD_MSG;
                break;
            case 2:
                fjcsName = "发展人名称";
                isFzrName = true;
                gsqdName = getIntent().getStringExtra("GSQD_NAME");
                mUrl = GlobalUrls.GET_FZR_NAME;
                break;
            case 3:
                fjcsName = "代收费发展商";
                isFzrName = false;
                mUrl = GlobalUrls.GET_DSF_FZS;
                break;
            case 4:
                fjcsName = "工单城区";
                isFzrName = false;
                mUrl = GlobalUrls.GET_GD_CITY;
                break;
            case 5:
                fjcsName = "代维护商";
                isFzrName = false;
                mUrl = GlobalUrls.GET_DWHS;
                break;
            case 6:
                fjcsName = "宽带账号";
                isFzrName = false;
                mUrl = GlobalUrls.GET_KUANDAI_NUM;
                break;
        }
        simSearch.setHint("请输入" + fjcsName);
        kdFjcsName.setText(fjcsName);

        initData();
    }

    protected void initData() {
        mKuanDaiFuJiaCanShuAdapter = new KuanDaiFuJiaCanShuAdapter(KuanDaiFuJiaCanShuActivity.this);
        kuandaiSimRecycler.enablePullRefresh(false);
        kuandaiSimRecycler.enableLoadMore(false); // 禁止加载
        mKuanDaiFuJiaCanShuAdapter.setOnItemClickListener(new KuanDaiFuJiaCanShuAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(KuanDaiFuJiaInfo.ListBean item) {
                Intent intent = new Intent();
                intent.putExtra("BackValue", item.getStr());
                intent.putExtra("BackID", item.getNum());
                intent.putExtra("BackType", canShuType);
                setResult(RESULT_OK, intent);
                finish();
                overridePendingTransition(R.anim.push_left_in, R.anim.push_right_out);
            }
        });

        // 设置Adapter
        kuandaiSimRecycler.setAdapter(mKuanDaiFuJiaCanShuAdapter);
        getData();
    }

    /**
     * 获取附加参数
     */
    public void getData() {
        rlProgress.setVisibility(View.VISIBLE);
        keyValue = simSearch.getText().toString().trim();
        MyApplication.getInstance().getHttpTools().getFuJiaCanShu(mUrl, isFzrName, keyValue, gsqdName,
                new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        try {
                            parseJson(response.body());
                        } catch (Exception e) {
                            e.printStackTrace();
                            Log.e("单宽带", "------数据解析异常------" + e.toString());
                            ToastUtils.showShortToast(KuanDaiFuJiaCanShuActivity.this, "数据解析异常");
                        }
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        ToastUtils.showShortToast(KuanDaiFuJiaCanShuActivity.this, "网络或服务器错误");
                    }

                    @Override
                    public void onFinish() {
                        super.onFinish();
                        if (kuandaiSimRecycler != null) {
                            kuandaiSimRecycler.postDelayed(() -> {
                                kuandaiSimRecycler.stopLoadMore();
                                kuandaiSimRecycler.stopRefresh();
                            }, 300);
                        }
                        rlProgress.setVisibility(View.GONE);
                    }
                });
    }

    /**
     * 解析数据
     *
     * @param json
     */
    private void parseJson(String json) {
        Gson gson = new Gson();
        KuanDaiFuJiaInfo mKuanDaiFuJiaInfo = gson.fromJson(json, KuanDaiFuJiaInfo.class);
        if (mKuanDaiFuJiaInfo != null) {
            int code = mKuanDaiFuJiaInfo.getCode();
            if (code == 200) {
                List<KuanDaiFuJiaInfo.ListBean> list = mKuanDaiFuJiaInfo.getList();
                if (list != null && list.size() > 0) {
                    mKuanDaiFuJiaCanShuAdapter.addAll(list);
                } else {
                    ToastUtils.showShortToast(KuanDaiFuJiaCanShuActivity.this, "没有更多数据了");
                }
            } else {
                ToastUtils.showShortToast(KuanDaiFuJiaCanShuActivity.this,
                        mKuanDaiFuJiaInfo.getMsg());
            }
        } else {
            ToastUtils.showShortToast(KuanDaiFuJiaCanShuActivity.this, "未获取到数据");
        }
    }
}
